package com.solomon.travel.common.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.solomon.travel.common.base.controller.BaseController;
import com.solomon.travel.common.model.ApiResponse;
import com.solomon.travel.common.model.Page;
import com.solomon.travel.common.po.Guide;
import com.solomon.travel.common.po.Hotel;
import com.solomon.travel.common.po.Image;
import com.solomon.travel.common.service.GuideService;
import com.solomon.travel.common.service.ImageService;
import com.solomon.travel.common.util.ConstantUtils;
import com.solomon.travel.common.util.IdUtils;
import com.solomon.travel.common.util.PageUtils;

@RestController
@RequestMapping("/web/guide")
public class GuideController extends BaseController {
	@Resource
	private GuideService guideService;
	
	@Resource
	private ImageService imageService;
	
	@RequestMapping("/add")
	public ApiResponse add(Guide guide,@RequestParam MultipartFile photo,@RequestParam MultipartFile certImage) throws Exception{
		guide.setCreatedAt(new Date());
		guide.setUpdatedAt(guide.getCreatedAt());
		guide.setUserId(currentUser().getObjectId());
		logger.info("guide--->"+JSON.toJSONString(guide));
		if (!photo.isEmpty()) {
			String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			 String originalName = photo.getOriginalFilename();
			 String fileName = System.currentTimeMillis()+originalName.substring(originalName.lastIndexOf("."));
			 String path = firstKey + "/guide/" + datePath + "/" + fileName;
			 ossClient.putObject(bucketName, path, photo.getInputStream());
			 guide.setPhotoUrl(ossurl+path);
			 logger.info("photo url--->"+guide.getPhotoUrl());
		}else{
			logger.info("photo is empty");
		}
		if (!certImage.isEmpty()) {
			String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			 String originalName = photo.getOriginalFilename();
			 String fileName = System.currentTimeMillis()+originalName.substring(originalName.lastIndexOf("."));
			 String path = firstKey + "/certImage/" + datePath + "/" + fileName;
			 ossClient.putObject(bucketName, path, certImage.getInputStream());
			 guide.setCertificateUrl(ossurl+path);
			 logger.info("certImage url--->"+guide.getCertificateUrl());
		}else{
			logger.info("certImage is empty");
		}
		guideService.add(guide);
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK,guide);
	} 
	
	@RequestMapping("/imageuploader")
	public ApiResponse upload(@RequestParam MultipartFile[] images,String objectId) throws Exception{
		if (!StringUtils.isEmpty(objectId)) {
			logger.info("get guideId --->"+objectId);
			if (images != null && images.length > 0) {
				logger.info("delete old guideImage.....");
				imageService.deleteGuideImages(objectId);
				for (MultipartFile hotelImage : images) {
					String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
					if (!hotelImage.isEmpty()) {
						 String originalName = hotelImage.getOriginalFilename();
						 String fileName = System.currentTimeMillis()+originalName.substring(originalName.lastIndexOf("."));
						 String path = firstKey + "/guide/" + datePath + "/" + fileName;
						 ossClient.putObject(bucketName, path, hotelImage.getInputStream());
						 Image image = new Image();
						 image.setObjectId(IdUtils.uuid());
						 image.setOrganizationId(objectId);
						 image.setType(ConstantUtils.ORGANIZE_TYPE_GUIDE);
						 image.setSubtype(ConstantUtils.IMAGE_SUBTYPE_GUIDE_DAITUAN);
						 image.setUrl(ossurl+path);
						 image.setCreatedAt(new Date());
						 image.setUpdatedAt(image.getCreatedAt());
						 imageService.add(image);
						 logger.info("guide daituan url--->"+image.getUrl());
					}
				}
			}
		}
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK);
	}
	
	@RequestMapping("/update")
	public ApiResponse updateHotel(Guide guide,@RequestParam MultipartFile photo,@RequestParam MultipartFile certImage) throws Exception{
		logger.info("guide--->"+JSON.toJSONString(guide));
		if (!photo.isEmpty()) {
			String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			 String originalName = photo.getOriginalFilename();
			 String fileName = System.currentTimeMillis()+originalName.substring(originalName.lastIndexOf("."));
			 String path = firstKey + "/guide/" + datePath + "/" + fileName;
			 ossClient.putObject(bucketName, path, photo.getInputStream());
			guide.setPhotoUrl(ossurl+path);
		}
		if (!certImage.isEmpty()) {
			String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			 String originalName = photo.getOriginalFilename();
			 String fileName = System.currentTimeMillis()+originalName.substring(originalName.lastIndexOf("."));
			 String path = firstKey + "/certImage/" + datePath + "/" + fileName;
			 ossClient.putObject(bucketName, path, certImage.getInputStream());
			 guide.setCertificateUrl(ossurl+path);
			 logger.info("certImage url--->"+guide.getCertificateUrl());
		}
		guideService.update(guide);
		return new ApiResponse(ConstantUtils.RESPONSE_CODE_OK,guide);
	}
	
	@RequestMapping("/list")
	public ApiResponse list(Page page) {
		int counts = guideService.count();
		page = PageUtils.createPage(page, counts);
		List<Guide> guides = guideService.listGuide(page);
		HashMap<String, Object> map = new HashMap<>();
		map.put("guides", guides);
		map.put("page", page);
		return new ApiResponse(200, map);
	}
}
